﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using Microsoft.Win32;
using System.Text.RegularExpressions;

namespace GroupChat2013.Common
{
    /// <summary>
    /// 网络管理类,用来请求网络资源
    /// </summary>
    public class WebClient
    {
        /// <summary>
        /// 获取请求的数据
        /// </summary>
        /// <param name="urlStr"></param>
        /// <returns></returns>
        public static string GetHttpData(string urlStr)
        {
            try
            {
                if (string.IsNullOrEmpty(urlStr))
                    return string.Empty;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlStr);
                if (request == null)
                    return string.Empty;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    return sr.ReadToEnd();
                }
            }
            catch { return string.Empty; }
        }

        /// <summary>
        /// 获取请求的数据
        /// </summary>
        /// <param name="urlStr"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public static string GetHttpData(string urlStr, CookieContainer cookie)
        {
            try
            {
                if (string.IsNullOrEmpty(urlStr))
                    return string.Empty;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlStr);
                if (request == null)
                    return string.Empty;

                request.CookieContainer = cookie;

                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                return retString;

            }
            catch { return string.Empty; }
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="urlStr"></param>
        /// <returns></returns>
        public static string PostHttpWithData(string urlStr, string sendData)
        {
            if (string.IsNullOrEmpty(urlStr) ||
                   string.IsNullOrEmpty(sendData))
                return string.Empty;

            return PostHttpData(urlStr, sendData);
        }

        public static string PostHttpData(string urlStr, string sendData, CookieContainer cookie)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlStr);
                if (request == null)
                    return string.Empty;

                request.CookieContainer = cookie;

                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";

                byte[] data = Encoding.UTF8.GetBytes(sendData);
                request.ContentLength = data.Length;
                using (Stream writer = request.GetRequestStream())
                {
                    writer.Write(data, 0, data.Length);
                }

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    return sr.ReadToEnd();
                }
            }
            catch { return string.Empty; }
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="urlStr"></param>
        /// <returns></returns>
        public static string DeleteHttpData(string urlStr, string sendData)
        {
            return DeleteHttpData(urlStr, sendData, null);
        }

        public static string DeleteHttpData(string urlStr, string sendData, CookieContainer cookie)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlStr);
                if (request == null)
                    return string.Empty;

                request.CookieContainer = cookie;

                request.Method = "DELETE";
                request.ContentType = "application/x-www-form-urlencoded";

                byte[] data = Encoding.UTF8.GetBytes(sendData);
                request.ContentLength = data.Length;
                using (Stream writer = request.GetRequestStream())
                {
                    writer.Write(data, 0, data.Length);
                }

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    return sr.ReadToEnd();
                }
            }
            catch { return string.Empty; }
        }
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="urlStr"></param>
        /// <returns></returns>
        public static string PostHttpData(string urlStr, string sendData)
        {
            return PostHttpData(urlStr, sendData, null);
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="urlStr"></param>
        /// <returns></returns>
        public static string PostHttpData(string urlStr)
        {
            return PostHttpData(urlStr, string.Empty);
        }

        /// <summary>
        /// 打开网页
        /// </summary>
        /// <param name="url"></param>
        public static void OpenBrowerURL(string url)
        {
            RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\");
            string s = key.GetValue("").ToString();

            Regex reg = new Regex("\"([^\"]+)\"");
            MatchCollection matchs = reg.Matches(s);

            string filename = "";
            if (matchs.Count > 0)
            {
                filename = matchs[0].Groups[1].Value;
                System.Diagnostics.Process.Start(filename, url);
            }
        }
    }
}
